const config = require('../util/config')
const logger = require('log4js').getLogger()
const ChromeServerIterator = require('../mng/chromeServerMng').Iterator
const taobaoLoginResolveMng = require('../mng/taobaoLoginResolveMng')

module.exports = (req, res) => {
    require('../util/service').handlePost(req, res, async (reqObj, resolve) => {
        if (config.domain == '') {
            require('../mng/chromeMng').getTaobaoBill().then(result => {
                logger.debug('[' + __filename + ':' + __line + ']', 'get taobao bill resove')
                resolve({
                    result: 1,
                    taobaoBill: result,
                    fromUsername: config.local.taobaoUsername
                })
            }, result => {
                if (result)
                    logger.error('[' + __filename + ':' + __line + ']', result)
                else
                    result = config.local.taobaoUsername + '系统错误'
                resolve({
                    result: -1,
                    msg: result,
                    fromUsername: config.local.taobaoUsername
                })
            })
        } else if (config.domain == 'zoro.site') {
            var promises = []
            if (reqObj.chromeServer) {
                promises.push(getFromChromeServer(reqObj.chromeServer))
            } else {
                var chromeServerIterator = new ChromeServerIterator()
                while (1) {
                    let chromeServer = chromeServerIterator.next()
                    if (!chromeServer)
                        break
                    promises.push(getFromChromeServer(chromeServer))
                }
            }
            if (promises.length == 0) {
                logger.error('[' + __filename + ':' + __line + ']', 'no chrome server')
                resolve({
                    result: -1,
                    msg: '系统错误'
                })
                return
            }
            Promise.all(promises).then(result => {
                // logger.debug('[' + __filename + ':' + __line + ']', result)
                var successCnt = 0, failedCnt = 0, failedUsernames = []
                result.forEach(item => {
                    if (item.result == 1)
                        successCnt++
                    else {
                        failedCnt++
                        failedUsernames.push(item.fromUsername)
                    }
                })
                resolve({
                    result: 1,
                    successCnt,
                    failedCnt,
                    failedUsernames
                })
            }, result => {
                if (result)
                    logger.error('[' + __filename + ':' + __line + ']', result)
                else
                    result = '系统错误'
                resolve({
                    result: -1,
                    msg: result
                })
            }).catch(err => {
                logger.error('[' + __filename + ':' + __line + ']', err)
                resolve({
                    result: -1,
                    msg: '系统错误'
                })
            })
        }
    })
}

function getFromChromeServer(chromeServer) {
    return new Promise(resolve => {
        require('../util/getHtml')({
            url: chromeServer + 'getTaobaoBill.do',
            data: JSON.stringify({})
        }).then(result => {
            var obj = JSON.parse(result)
            if (obj.result == 1) {
                require('../task/getTaobaoBillTask1').getTaobaoBillResolve(obj.taobaoBill)
                if (taobaoLoginResolveMng.hasResolve) {
                    taobaoLoginResolveMng.hasResolve = false
                    taobaoLoginResolveMng.resolve()
                }
            }
            resolve(obj)
        }, result => {
            logger.error('[' + __filename + ':' + __line + ']', result)
            resolve({
                result: -1,
                msg: '系统错误'
            })
        })
    })
}
